home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part2 / 10126 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.3 KB

  1. Path: sdrc.com!thor!scjones
  2. From: larry.jones@sdrc.com (Larry Jones)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: #define "creating" strings ?
  5. Date: 15 Mar 1996 18:10:35 GMT
  6. Organization: SDRC Engineering Services
  7. Distribution: world
  8. Message-ID: <4icbqr$qlt@info1.sdrc.com>
  9. References: <Do9tsI.H2t@undergrad.math.uwaterloo.ca>
  10. NNTP-Posting-Host: thor.sdrc.com
  11. Originator: scjones@thor
  12.  
  13. In article <Do9tsI.H2t@undergrad.math.uwaterloo.ca>, crpalmer@solo.uwaterloo.ca (Chris Palmer) writes:
  14. > #define MAKESTRING(x) "x"
  15. > which does in fact produce strings that have the value of x substituted.
  16. > [eg: MAKESTRING(foo) gets processed into "foo"].
  17. > I've verified this on a DEC Alpha (cc) and the GNU C compiler..
  18. > Is this in fact a supported behaviour that should be portable to most
  19. > C compilers?
  20.  
  21. No.  This was a bug in one early preprocessor that has been widely
  22. copied; a careful reading of K&R clearly forbids it, as does the
  23. ANSI/ISO C Standard (if you'd used gcc -ansi, you would know that).  The
  24. ANSI ``stringizing'' operator is the right way to do this:
  25.  
  26.     #define STRINGIZE(x) #x
  27.     #define MAKESTRING(x) STRINGIZE(x)
  28.  
  29.     #define XYZ 123
  30.  
  31.     STRINGIZE(XYZ);      /* "XYZ" */
  32.     MAKESTRING(XYZ);  /* "123" */
  33. ----
  34. Larry Jones, SDRC, 2000 Eastman Dr., Milford, OH  45150-2789  513-576-2070
  35. larry.jones@sdrc.com
  36. Shut up and go get me some antiseptic. -- Calvin
  37.